/*
 * @Author: wangcb
 * @Email: wangcbgg@163.com
 * @Date: 2021-10-21 17:05:52
 * @LastEditTime: 2021-11-17 11:13:50
 * 科大语音监听封装
 */
/**
 * getcmd
 * @param {Function} cb 回调
 * @param {Listener} listener 监听器
 */
function getcmd(cb, listener) {
    const xhm = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    xhm.open("GET", "http://127.0.0.1:44570/getcmd", true);
    listener.uuid && xhm.setRequestHeader("uuid", listener.uuid);
    xhm.send();
    xhm.onreadystatechange = function () {
        if (4 == this.readyState) {
            if (200 == this.status && this.responseText) {
                let jsonText;
                try {
                    cb && cb(JSON.parse(this.responseText), listener);
                }
                catch (t) {
                    if (this.responseText != "ok") {
                        try {
                            jsonText = this.responseText.replace(/'/g, '"');
                            cb && cb(JSON.parse(jsonText), listener);
                        }
                        catch (error) {
                            window.console && window.console.log(t.name + ": " + t.message);
                        }
                    }
                }
            }
            const t = setTimeout(() => {
                clearTimeout(t), getcmd(cb, listener);
            }, 200);
        }
    };
}
/**
 * 通用post请求
 * @param {string} url 请求链接后缀
 * @param {*} data 数据
 * @param {number} uuid 时间戳
 */
function post(url, data, uuid) {
    const xhm = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    xhm.open(url === 'getcmd' ? 'GET' : 'POST', `http://127.0.0.1:44570/${url}`, true);
    xhm.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    uuid && xhm.setRequestHeader('uuid', uuid);
    data && xhm.send(JSON.stringify(data || {}));
    xhm.onreadystatechange = function () {
        if (4 == this.readyState && 200 != this.status) {
            const t = setTimeout(() => {
                clearTimeout(t), uuid = Date.now(), post(url, data, uuid);
            }, 1000);
        }
    };
    return uuid;
}
/**
 * 语音监听器
 * @class Listener
 */
export class Listener {
    /**
     * 语音监听构造
     * @param {CallBack} cb 语音回调
     */
    constructor(cb) {
        /** 时间戳 */
        this.uuid = 0;
        getcmd(cb, this);
    }
    /**
     * 场景注册
     * @param scene
     * @function
     */
    regist(scene) {
        this.uuid = post('postkeyword', scene, Date.now());
    }
    /**
     * 同步界面状态
     * @param {*} viewState 界面状态
     */
    syncViewState(viewState) {
        post('syncviewstate', viewState);
    }
    /**
     * 同步频道信息
     * @param {*} channelInfo 频道信息
     */
    syncChannelInfo(channelInfo) {
        post('syncchannelinfo', channelInfo);
    }
    /**
     * 同步客户端信息
     * @param {*} clientInfo 客户端信息
     */
    syncClientInfo(clientInfo) {
        post('syncclientinfo', clientInfo);
    }
    /**
     * 发送反馈消息
     * @param {string} info 反馈消息
     */
    postFeedBack(info) {
        post('postFeedBack', info);
    }
}
